package com.nameless.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nameless.dao.BaseAreaDao;
import com.nameless.entity.BaseArea;
import com.nameless.service.BaseAreaService;
import com.nameless.vo.ProvinceCityCounty;
import com.nameless.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author keluoyisi
 * @program: car-parent
 * @description:
 * @date 2023-02-27 18:34:08
 */
@Service
@SuppressWarnings("all")
public class BaseAreaServiceImpl extends ServiceImpl<BaseAreaDao, BaseArea> implements BaseAreaService {
    @Autowired
    private BaseAreaDao baseAreaDao;

    @Override
    public Result parentId(Integer parentId) {
        QueryWrapper<BaseArea> wrapper = new QueryWrapper<>();
        wrapper.eq("parentid",parentId);
        List<BaseArea> list = baseAreaDao.selectList(wrapper);
        return new Result(200,"成功",list);
    }
    private List<BaseArea> getChildres(List<BaseArea> baseAreas, BaseArea item) {
        List<BaseArea> collect = baseAreas.stream().filter(items -> items.getParentid().equals(item.getBaseAreaid()))
                .map(items -> {
                    items.setChildren(getChildres(baseAreas, items));
                    return items;
                }).collect(Collectors.toList());
        return collect;
    }
    @Override
    public Result getProvinceCityCounty() {
        Map<String,List<Map<Integer, String>>> map=new HashMap<>();
        //所有的省市区
        List<BaseArea> baseAreas = baseAreaDao.selectList(null);

        //所有的省

        List<BaseArea> province_list = baseAreas.stream()
                .filter(item -> item.getParentid() == 0).collect(Collectors.toList());


        List<BaseArea> city_list=new ArrayList<>();
        for(BaseArea province:province_list){
              for(BaseArea area:baseAreas){
                    if(area.getParentid().equals(province.getBaseAreaid())){
                         city_list.add(area);
                    }
              }
        }

        List<BaseArea> county_list=new ArrayList<>();
        for(BaseArea province:city_list){
            for(BaseArea area:baseAreas){
                if(area.getParentid().equals(province.getBaseAreaid())){
                    county_list.add(area);
                }
            }
        }

        StringBuilder builder = new StringBuilder();
        builder.append("{");

        for (BaseArea area : county_list) {
            builder.append(area.getBaseAreaid()+": '"+area.getName()+"',");
        }

        builder.append("}");
        System.out.println(builder);
        List<Map<Integer, String>> collect = province_list.stream().map(item -> {
            Map<Integer, String> p = new HashMap<>();
            p.put(item.getBaseAreaid(), item.getName());
            return p;
        }).collect(Collectors.toList());

        List<Map<Integer, String>> collect1 = city_list.stream().map(item -> {
            Map<Integer, String> p = new HashMap<>();
            p.put(item.getBaseAreaid(), item.getName());
            return p;
        }).collect(Collectors.toList());

        List<Map<Integer, String>> collect2 = county_list.stream().map(item -> {
            Map<Integer, String> p = new HashMap<>();
            p.put(item.getBaseAreaid(), item.getName());
            return p;
        }).collect(Collectors.toList());


//        System.out.println(collect);
//        System.out.println(collect1);
//        System.out.println(collect2);
        map.put("province_list",collect);
        map.put("city_list",collect1);
        map.put("county_list",collect2);

        return new Result(200,"成功",map);

//        //获取到所有的省
//        QueryWrapper<BaseArea> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("parentid",0);
//        List<BaseArea> provinceList = baseAreaDao.selectList(wrapper1);
//
//        //获取到所有的市
//        ArrayList<BaseArea> cityList = new ArrayList<>();
//        for (BaseArea area : provinceList) {
//            Integer baseAreaid = area.getBaseAreaid();
//            QueryWrapper<BaseArea> wrapper = new QueryWrapper<>();
//            wrapper1.eq("parentid",baseAreaid);
//            List<BaseArea> list = baseAreaDao.selectList(wrapper);
//            for (BaseArea baseArea : list) {
//                cityList.add(baseArea);
//            }
//        }
//
//
//        //获取到所有的县、区
//        ArrayList<BaseArea> countyList = new ArrayList<>();
//        for (BaseArea city : cityList) {
//            Integer baseAreaid = city.getBaseAreaid();
//            QueryWrapper<BaseArea> wrapper = new QueryWrapper<>();
//            wrapper1.eq("parentid",baseAreaid);
//            List<BaseArea> list = baseAreaDao.selectList(wrapper);
//            for (BaseArea baseArea : list) {
//                countyList.add(baseArea);
//            }
//        }
//
//        ProvinceCityCounty provinceCityCounty = new ProvinceCityCounty(provinceList,cityList,countyList);
//        return new Result(200,"成功",provinceCityCounty);
    }
}
